package com.app.beans;

import com.app.model.Equipement;
import com.app.model.Service;
import com.app.interfaces.interfaceService;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import org.richfaces.component.UIDataTable;

/**
 *
 * @author Krachleur
 */

public class EquipementBean extends MessageBean implements Serializable {

    private Equipement equip;
    private interfaceService equipService;
    private interfaceService servService;
    private List equip_list = new ArrayList();
    private List service_list = new ArrayList();
    private Hashtable servlist = new Hashtable();
    private String current_service;
    private boolean init;
    private Integer index = 0;
    private String message;
    private boolean nouveau;
    private UIDataTable dataTable;

    public void viderChamps(){
        message = "";
        changerEtat();
        this.equip = new Equipement();
        nouveau = true;
    }

    public void annuler(){
        message = "";
        equip_list = (List)getEquip_list();
        equip = (Equipement)equip_list.get(index);
        changerEtat();
    }

    public void chargerCombo(){
        current_service = this.equip.getService().getServCode()+ "-" +this.equip.getService().getServLib();
    }

    /** Creates a new instance of EquipementBean */
    public EquipementBean() {
        this.equip = new Equipement();
        this.equip.setService(new Service());
    }

    public void create(){
        if(nouveau){
            try{
                this.equip.setService((Service)servlist.get(current_service));
                this.equipService.save(this.equip);
                message = mess_insert_true;
                style_message = "valid_message";
                chargerCombo();
            }catch(Exception e){
                e.printStackTrace();
                message = mess_op_false;
                style_message = "err_message";
            }
        } else {
            try{
                this.equip.setService((Service)servlist.get(current_service));
                this.equipService.modify(this.equip);
                message = mess_modif_true;
                style_message = "valid_message";
                chargerCombo();
            }catch(Exception e){
                e.printStackTrace();
                message = mess_op_false;
                style_message = "err_message";
            }
        }
        changerEtat();
    }

    public void modifierLigne(){
        this.equip = (Equipement)dataTable.getRowData();
        chargerCombo();
        message = "";
        nouveau = false;
        changerEtat();
    }

    public void modifier(){
        message = "";
        nouveau = false;
        changerEtat();
    }

    public void supprimer(){
        try{
            this.equipService.delete(equip);
            message = mess_del_true;
            style_message = "valid_message";
            if(index > 0){
                index--;
            }
            equip_list = (List)getEquip_list();
            if(!equip_list.isEmpty()){
                equip = (Equipement)equip_list.get(index);
                chargerCombo();
            } else {
                    viderChamps();
                changerEtat();
            }
        } catch(Exception e){
            e.printStackTrace();
            message = mess_op_false;
        }
    }

    public String getCurrent_service() {
        return current_service;
    }

    public UIDataTable getDataTable() {
        return dataTable;
    }

    public Equipement getEquip() {
        return equip;
    }

    public interfaceService getEquipService() {
        return equipService;
    }

    public List getEquip_list() {
        equip_list = this.equipService.findAll();
        return equip_list;
    }

    public Integer getIndex() {
        return index;
    }

    public boolean isInit() {
        message = "";
        getServlist();
        getEquip_list();
        if(!(equip_list.isEmpty())){
            equip_list = (List)getEquip_list();
            equip = (Equipement)equip_list.get(0);
            chargerCombo();
        }
        etat = true;
        invetat = !etat;
        return init;
    }

    public String getMessage() {
        return message;
    }

    public boolean isNouveau() {
        return nouveau;
    }

    public interfaceService getServService() {
        return servService;
    }

    public List getService_list() {
        return service_list;
    }

    public Hashtable getServlist() {
        service_list.clear();
        servlist.clear();
        List l = this.servService.findAll();
        for(Iterator it = l.iterator(); it.hasNext();){
            Service srv = (Service)l.get(l.indexOf(it.next()));
            service_list.add(srv.getServCode() + "-" +srv.getServLib());
            servlist.put(srv.getServCode() + "-" + srv.getServLib(), srv);
        }
        return servlist;
    }

    public void setCurrent_service(String current_service) {
        this.current_service = current_service;
    }

    public void setDataTable(UIDataTable dataTable) {
        this.dataTable = dataTable;
    }

    public void setEquip(Equipement equip) {
        this.equip = equip;
    }

    public void setEquipService(interfaceService equipService) {
        this.equipService = equipService;
    }

    public void setEquip_list(List equip_list) {
        this.equip_list = equip_list;
    }

    public void setIndex(Integer index) {
        this.index = index;
    }

    public void setInit(boolean init) {
        this.init = init;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void setNouveau(boolean nouveau) {
        this.nouveau = nouveau;
    }

    public void setServService(interfaceService servService) {
        this.servService = servService;
    }

    public void setService_list(List service_list) {
        this.service_list = service_list;
    }

    public void setServlist(Hashtable servlist) {
        this.servlist = servlist;
    }


}